import socket


def main():
    tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)

    tcp_server.bind(('',8080))

    tcp_server.listen(128)

    while True:
        new_socket, new_address = tcp_server.accept()
        print(f"{str(new_address)}已经上线了")

        recv_data = new_socket.recv(4096)

        if not recv_data:
            print(f"{str(new_address)}已经下线了")
            new_socket.close()
            return
        with open("./static/web.jpg", "rb") as file:
            file_data = file.read()

        # http_data = 'HTTP/1.1 200 OK\r\nServer: PWS\r\n\r\n' + 'hello world'
        http_data = 'HTTP/1.1 200 OK\r\nServer: PWS\r\n\r\n'.encode() + file_data
        new_socket.send(http_data)

        new_socket.close()

if __name__ == '__main__':
    main()